import PlayerStorageManager, { PlayerStorage } from "./PlayerStorageManager";



const {ccclass, property} = cc._decorator;

@ccclass
export default class SoundManager {

    static _instance:SoundManager;

    static get Instance()
    {
        return this._instance || (this._instance = new SoundManager());
    }

    effectAudio = {
        volume: 0.8,//音量
        Id: null,
        open:true,//音效是否开启
    };

    
    musicAudio = {
        volume: 0.8,//音量
        Id: null,
        open:true,//音乐是否开启
    };
    isInit = false;

    private _init()
    {
        if(!this.isInit)
        {
            let musicOpen = PlayerStorageManager.Instance.getStorage(PlayerStorage.MusicOpen);
           //  cc.sys.localStorage.getItem(PlayerStorage.MusicOpen);
            if(musicOpen == "false")this.musicAudio.open = false;
            console.error(this.musicAudio.open);
            let effectOpen =PlayerStorageManager.Instance.getStorage(PlayerStorage.EffectOpen);
            if( effectOpen == "false")this.effectAudio.open = false;
            this.isInit = true;
        }
    }

    PlayEffect(soundName?:string)
    {  
        this._init();
        if(this.effectAudio.open == true) 
        {  
            if(!soundName)soundName = "buttonClicked";
            //if (this.effectAudio.Id !=null) {cc.audioEngine.stop(this.effectAudio.Id);}

            this.effectAudio.Id  = cc.audioEngine.play(cc.url.raw('resources/Audio/' + soundName + '.mp3'), false, this.effectAudio.volume);
        }
      
    }

    PlayMusic( soundName?:string,loop?:boolean)
    {
        this._init();
        if(this.musicAudio.open == true)  
        { 
            console.log("没走这里吧")
            if(!loop)loop = true;
            if (this.musicAudio.Id !=null) cc.audioEngine.pause(this.musicAudio.Id );
            this.musicAudio.Id  = cc.audioEngine.play(cc.url.raw('resources/Audio/' + soundName + '.mp3'), loop, this.musicAudio.volume);
            // v.log(" this.musicAudio.Id 1 ",this.musicAudio.Id );
        }
    }


    // Stop( playingId:number)
    // {
    //     if (playingId ) cc.audioEngine.stop(playingId);
    //     v.log(" state ", cc.audioEngine.getState(playingId));
    // }

    // StopEffect()
    // {
    //     if (this.effectAudio.Id ) cc.audioEngine.stop(this.effectAudio.Id );
    // }

    // StopMusic()
    // {
    //     if (this.musicAudio.Id ) cc.audioEngine.pause(this.musicAudio.Id );
    // }

    // StopAll()
    // {
    //     cc.audioEngine.stopAll();
    // }


    // SetEffectVolume( volume:number)
    // {
    //     if (this.effectAudio.Id ) cc.audioEngine.setVolume(this.effectAudio.Id,volume);
    // }

    // SetMusicVolume( volume:number)
    // {
    //     if (this.musicAudio.Id ) cc.audioEngine.setVolume(this.musicAudio.Id,volume);
    // }


    // GetMusicVolume():number
    // {
    //     return this.musicAudio.volume;
    // }

    // GetEffectVolume():number
    // {  
    //     return this.effectAudio.volume;
    // }

    SetStorage(itemName:string,itemValue:any)
    {
        // v.log("  itemName  ",itemName," itemValue ",itemValue);
        // v.log("  this.musicAudio.Id  ",this.musicAudio);
        // v.log("  this.effectAudio.Id  ",this.effectAudio);
        PlayerStorageManager.Instance.setStorage(itemName,itemValue)
        //cc.sys.localStorage.setItem(itemName,itemValue);
        switch(itemName)
        {
            case PlayerStorage.MusicOpen:
            this.musicAudio.open = itemValue;
            if(itemValue)
            {
                if (this.musicAudio.Id!=null ) cc.audioEngine.resume(this.musicAudio.Id );
            }
            else
            {
                if (this.musicAudio.Id !=null) 
                {
                    cc.audioEngine.pause(this.musicAudio.Id );
                }
            }
            break;
            case PlayerStorage.EffectOpen:
            this.effectAudio.open = itemValue;   break;
        }
    }
}


